package com.example.d6_homework.homework06;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Test {
    // 1.创建一个集合存储54张牌
    private static List<Card> allCards = new ArrayList<>();

    // 2.使用静态代码块初始化54张牌
    static {
        // 3.准备点数
        String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        // 4.准备花色
        String[] colors = {"♥", "♦", "♣", "♠"};
        // 5.组合点数和花色成为牌对象加入到集合中
        int num = 0;
        for (String size : sizes) {
            num++;
            for (String color : colors) {
                // 6.创建牌对象
                Card card = new Card(size, color, num);
                // 7.添加到集合中
                allCards.add(card);
            }
        }
        // 8.添加大小王
        Collections.addAll(allCards, new Card("", "🃏", ++num), new Card("", "👲", ++num));
    }

    public static void main(String[] args) {
        // 1.定义四个集合存储三个玩家的牌和底牌
        List<Card> player1 = new ArrayList<>();
        List<Card> player2 = new ArrayList<>();
        List<Card> player3 = new ArrayList<>();
        List<Card> bottomCards = new ArrayList<>();

        // 2.直接打乱牌中的顺序
        Collections.shuffle(allCards);

        // 3.按照顺序给三个玩家发牌
        for (int i = 0; i < 51; i++) {
            if (i % 3 == 0) {
                player1.add(allCards.get(i));
            } else if (i % 3 == 1) {
                player2.add(allCards.get(i));
            } else {
                player3.add(allCards.get(i));
            }
        }

        // 4.给底牌发3张牌
        bottomCards.addAll(allCards.subList(51, allCards.size()));

        // 5.对玩家手中的牌进行排序
        printCards("玩家一", player1);
        printCards("玩家二", player2);
        printCards("玩家三", player3);
        printCards("底牌", bottomCards);
    }

    /**
     * 对牌集合进行排序并返回排序后的列表
     *
     * @param cards 牌集合
     * @return 排序后的牌集合
     */
    public static List<Card> sortCards(List<Card> cards) {
        return cards.stream().sorted(Comparator.comparingInt(Card::getNum)).collect(Collectors.toList());
    }

    /**
     * 打印玩家或底牌的信息
     *
     * @param playerName 玩家名或"底牌"
     * @param cards      要打印的牌集合
     */
    public static void printCards(String playerName, List<Card> cards) {
        System.out.print(playerName + "的牌是：");
        for (Card card : sortCards(cards)) {
            System.out.print(card + " ");
        }
        System.out.println(); // 换行以区分不同玩家或底牌
    }
}
